package com.basic.common.utils;

import java.security.MessageDigest;
import java.util.Base64;

public class SHA1Encryption {

    public static void main(String[] args) {
        String input = "Kn12345670@";
        String hexDigest = toHexString(sha1(input));
        String base64Digest = toBase64String(sha1(input));
        String stringDigest = toString(sha1(input));

        System.out.println("Hex Digest: " + hexDigest);
        System.out.println("Base64 Digest: " + base64Digest);
        System.out.println("String Digest: " + stringDigest);
    }

    public static byte[] sha1(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            return digest.digest(input.getBytes());
        } catch (Exception e) {
            throw new RuntimeException("SHA-1 algorithm not found", e);
        }
    }

    public static String toHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }

    public static String toBase64String(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String toString(byte[] bytes) {
        return new String(bytes);
    }
}